// ============================================================================
//
// LCP-net framework by Pierre CHATEL chatelp@gmail.com
// Distributed under GPLv3
// Copyright (C) THALES Land & Joint Systems, 2008, 2009. All rights reserved
//
// ============================================================================

/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

import com.thalesgroup.preferenceFramework.models.LCPnet.CLPTLine;
import com.thalesgroup.preferenceFramework.models.LCPnet.LNodeValue;
import com.thalesgroup.preferenceFramework.models.LCPnet.LNodeValueUtility;
import com.thalesgroup.preferenceFramework.models.LCPnet.LinguisticSubset;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetCombinationException;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.CLPTLineFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.Fragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.FragmentsPackage;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LNodeValueFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LNodeValueUtilityFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LinguisticSubsetFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.impl.LNodeValueUtilityImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>LNode Value Utility Fragment</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.LNodeValueUtilityFragmentImpl#getFragmentName <em>Fragment Name</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.LNodeValueUtilityFragmentImpl#isAntiFragment <em>Anti Fragment</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.LNodeValueUtilityFragmentImpl#getBaseLNodeValueUtility <em>Base LNode Value Utility</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class LNodeValueUtilityFragmentImpl extends LNodeValueUtilityImpl implements LNodeValueUtilityFragment {
	/**
	 * The default value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected static final String FRAGMENT_NAME_EDEFAULT = "";
	/**
	 * The cached value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected String fragmentName = FRAGMENT_NAME_EDEFAULT;
	/**
	 * The default value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected static final boolean ANTI_FRAGMENT_EDEFAULT = false;
	/**
	 * The cached value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected boolean antiFragment = ANTI_FRAGMENT_EDEFAULT;
	/**
	 * The cached value of the '{@link #getBaseLNodeValueUtility() <em>Base LNode Value Utility</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBaseLNodeValueUtility()
	 * @generated
	 * @ordered
	 */
	protected LNodeValueUtility baseLNodeValueUtility;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected LNodeValueUtilityFragmentImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return FragmentsPackage.Literals.LNODE_VALUE_UTILITY_FRAGMENT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getFragmentName() {
		return fragmentName;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFragmentName(String newFragmentName) {
		String oldFragmentName = fragmentName;
		fragmentName = newFragmentName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME, oldFragmentName, fragmentName));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isAntiFragment() {
		return antiFragment;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAntiFragment(boolean newAntiFragment) {
		boolean oldAntiFragment = antiFragment;
		antiFragment = newAntiFragment;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT, oldAntiFragment, antiFragment));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public LNodeValueUtility getBaseLNodeValueUtility() {
		if (baseLNodeValueUtility != null && baseLNodeValueUtility.eIsProxy()) {
			InternalEObject oldBaseLNodeValueUtility = (InternalEObject)baseLNodeValueUtility;
			baseLNodeValueUtility = (LNodeValueUtility)eResolveProxy(oldBaseLNodeValueUtility);
			if (baseLNodeValueUtility != oldBaseLNodeValueUtility) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__BASE_LNODE_VALUE_UTILITY, oldBaseLNodeValueUtility, baseLNodeValueUtility));
			}
		}
		return baseLNodeValueUtility;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public LNodeValueUtility basicGetBaseLNodeValueUtility() {
		return baseLNodeValueUtility;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBaseLNodeValueUtility(LNodeValueUtility newBaseLNodeValueUtility) {
		LNodeValueUtility oldBaseLNodeValueUtility = baseLNodeValueUtility;
		baseLNodeValueUtility = newBaseLNodeValueUtility;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__BASE_LNODE_VALUE_UTILITY, oldBaseLNodeValueUtility, baseLNodeValueUtility));
	}

	public void applyFragment(EObject context) throws LCPnetCombinationException {
		if(context instanceof CLPTLineFragment) {
			//Fragment contained in context --> apply fragment

			LNodeValueUtility baseLNodeValueUtility = this.getBaseLNodeValueUtility();
			
			if(this.isAntiFragment()) {
				CLPTLineFragment callingClptLineFragment = (CLPTLineFragment) context;
				CLPTLine baseCLPTLine = callingClptLineFragment.getBaseCLPTLine();
				baseCLPTLine.getUtility().remove(baseLNodeValueUtility);
				return;
			}
			
			LNodeValue newNodeValue = this.getNodeValue();
			if(newNodeValue != null) {
				if(newNodeValue instanceof LNodeValueFragment) {
					((LNodeValueFragment)newNodeValue).applyFragment(this);
				} else {
					baseLNodeValueUtility.setNodeValue(newNodeValue);
				}
			}
			
			LinguisticSubset newUtility = this.getUtility();
			if(newUtility != null) {
				if(newUtility instanceof LinguisticSubsetFragment) {
					((LinguisticSubsetFragment)newUtility).applyFragment(this);
				} else {
					baseLNodeValueUtility.setUtility(newUtility);
				}
			}
			
		} else {
			throw new LCPnetCombinationException("Combination error: unexpected calling context for LNodeValueUtility fragment");
		
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME:
				return getFragmentName();
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT:
				return isAntiFragment() ? Boolean.TRUE : Boolean.FALSE;
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__BASE_LNODE_VALUE_UTILITY:
				if (resolve) return getBaseLNodeValueUtility();
				return basicGetBaseLNodeValueUtility();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME:
				setFragmentName((String)newValue);
				return;
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(((Boolean)newValue).booleanValue());
				return;
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__BASE_LNODE_VALUE_UTILITY:
				setBaseLNodeValueUtility((LNodeValueUtility)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME:
				setFragmentName(FRAGMENT_NAME_EDEFAULT);
				return;
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(ANTI_FRAGMENT_EDEFAULT);
				return;
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__BASE_LNODE_VALUE_UTILITY:
				setBaseLNodeValueUtility((LNodeValueUtility)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME:
				return FRAGMENT_NAME_EDEFAULT == null ? fragmentName != null : !FRAGMENT_NAME_EDEFAULT.equals(fragmentName);
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT:
				return antiFragment != ANTI_FRAGMENT_EDEFAULT;
			case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__BASE_LNODE_VALUE_UTILITY:
				return baseLNodeValueUtility != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (derivedFeatureID) {
				case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (baseFeatureID) {
				case FragmentsPackage.FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.LNODE_VALUE_UTILITY_FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (fragmentName: ");
		result.append(fragmentName);
		result.append(", antiFragment: ");
		result.append(antiFragment);
		result.append(')');
		return result.toString();
	}

} //LNodeValueUtilityFragmentImpl
